package com.github.common.util.math;

public class NumberCastUtil {
    public static long toLong(Object value) {
        return toNumber(value).longValue();
    }

    public static int toInteger(Object value) {
        return toNumber(value).intValue();
    }

    public static float toFloat(Object value) {
        return toNumber(value).floatValue();
    }

    public static double toDouble(Object value) {
        return toNumber(value).doubleValue();
    }

    private static Number toNumber(Object value) {
        Number number;
        try {
            if (value instanceof Number) {
                number = (Number) value;
            } else {
                number = Double.valueOf(value.toString());
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(value.toString(), e);
        }
        return number;
    }
}
